﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BigMonitoringWarningApi.Tool.Extension
{
    public static partial class GyctExtention
    {
        /// <summary>
        /// 返回最小默认时间1970-01-01
        /// </summary>
        /// <returns></returns>
        public static DateTime ToDateMinDefault() => DateTime.Parse("1970-01-01 00:00:00");

        /// <summary>
        /// 字符串转时间 错误默认返回最小时间1970-01-01
        /// </summary>
        /// <param name="time">时间</param>
        /// <param name="type">选择无返回值的值,返回最小时间还是当前时间还是为空,默认返回空</param>
        /// <returns></returns>
        public static DateTime? ToDateTime(this object time, TimeType type = TimeType.NullTime)
        {
            if (DateTime.TryParse(time + "", out DateTime datetime))
            {
                return datetime;
            }
            switch (type)
            {
                case TimeType.NowDate:
                    return DateTime.Now;
                case TimeType.NullTime:
                    return null;
                default:
                    return ToDateMinDefault();
            }
        }
    }
    /// <summary>
    /// 时间类型 
    /// </summary>
    public enum TimeType
    {
        /// <summary>
        /// 现在时间
        /// </summary>
        NowDate = 0,
        /// <summary>
        /// 最小时间
        /// </summary>
        MinDate = 1,
        /// <summary>
        /// 返回空,注意需要时间为可空类型 DateTime?
        /// </summary>
        NullTime=2
    }
}
